//
//  mm_multipleCustomerToAllocateEditViewController.m
//  HondaPro_2
//
//  Created by Eric on 13-7-18.
//  Copyright (c) 2013年 glendale. All rights reserved.
//

#import "mm_multipleCustomerToAllocateEditViewController.h"

@interface mm_multipleCustomerToAllocateEditViewController ()

@end

@implementation mm_multipleCustomerToAllocateEditViewController

@synthesize nRowAllocationListToSel = _nRowAllocationListToSel;
@synthesize nRowCustomerListToSel = _nRowCustomerListToSel;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
        [self.view setFrame:CGRectMake(0, 0, 500, 600)];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    //注册键盘消息
    [self registerNotificationForKeyBoard];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidUnload
{
    [self setCustomerIdLbl:nil];
    [self setCustomerNameLbl:nil];
    [self setRepairMoneyTxt:nil];
    [self setProportion:nil];
    [super viewDidUnload];
}

- (IBAction)deleteTheCustomerBtn:(UIButton *)sender
{
    //进行删除操作
    bDeleteBtnFlag = YES;
    
    //提示删除成功
    UIAlertView *alerVeiw = [[UIAlertView alloc] initWithTitle:@"提示信息" message:@"删除成功！" delegate:self cancelButtonTitle:@"我知道了" otherButtonTitles:nil, nil];
    [alerVeiw show];
}

//当退出该界面的时候保存数据，有可能是修改，也有可能是删除
-(void)saveData
{
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    
    NSMutableDictionary *dicCustomerTmp = [NSMutableDictionary dictionaryWithDictionary:[[[modelIns.arrAllocationList objectAtIndex:self.nRowAllocationListToSel] objectForKey:MM_CUSTOMER_LIST_KEY_STR_] objectAtIndex:self.nRowCustomerListToSel]];
    
    [dicCustomerTmp setObject:[NSString stringWithFormat:@"%0.2f",[self.repairMoneyTxt.text floatValue]] forKey:MM_CUSTOMER_REPAIR_MONEY_KEY_STR_];
    
    //保存数据
    NSMutableArray *arrCustomerListTmp = [NSMutableArray arrayWithArray:[[modelIns.arrAllocationList objectAtIndex:self.nRowAllocationListToSel] objectForKey:MM_CUSTOMER_LIST_KEY_STR_]];
    
    if (bDeleteBtnFlag == YES)
    {
        [arrCustomerListTmp removeObjectAtIndex:self.nRowCustomerListToSel];
    }
    else
    {
        [arrCustomerListTmp setObject:dicCustomerTmp atIndexedSubscript:self.nRowCustomerListToSel];
    }
    
    NSMutableDictionary *dicAllocationTmp = [NSMutableDictionary dictionaryWithDictionary:[modelIns.arrAllocationList objectAtIndex:self.nRowAllocationListToSel]];
    [dicAllocationTmp setObject:arrCustomerListTmp forKey:MM_CUSTOMER_LIST_KEY_STR_];
    
    modelIns.arrAllocationList = [NSMutableArray arrayWithArray:modelIns.arrAllocationList];
    [modelIns.arrAllocationList setObject:dicAllocationTmp atIndexedSubscript:_nRowAllocationListToSel];
    
    //移除消息
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    textField.keyboardType = UIKeyboardTypeNumberPad;
    
    int offset = 150;//键盘高度350
    NSTimeInterval animationDuration = 0.30f;
    [UIView beginAnimations:@"ResizeForKeyBoard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    float width = self.view.superview.frame.size.width;
    float height = self.view.superview.frame.size.height;
    if(offset > 0)
    {
        CGRect rect = CGRectMake(0.0f, -offset,width,height);
        self.view.superview.frame = rect;
    }
    [UIView commitAnimations];
    
    if (textField == self.repairMoneyTxt)
    {
        [self.proportion setEnabled:NO];
        [self.proportion setBackgroundColor:[UIColor lightGrayColor]];
    }
    else
    {
        [self.repairMoneyTxt setEnabled:NO];
        [self.repairMoneyTxt setBackgroundColor:[UIColor lightGrayColor]];
    }
}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    float totalMoney = [self totalMoneyByAllocationList:_nRowAllocationListToSel];
    
    if (textField == self.repairMoneyTxt)
    {
        //设置比例
        float fRepairMoney = [self.repairMoneyTxt.text floatValue];
        self.proportion.text = [NSString stringWithFormat:@"%i",(NSInteger)(fRepairMoney*100/totalMoney)];
    }
    else
    {
        //设置金额
        float fRepairMoney = totalMoney*[self.proportion.text floatValue]/100.0;
        self.repairMoneyTxt.text = [NSString stringWithFormat:@"%0.2f",fRepairMoney];
    }
}


//注册键盘消息
-(void)registerNotificationForKeyBoard
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setViewNormal) name:UIKeyboardWillHideNotification object:nil];
}

//视图回归正常状态
-(void)setViewNormal
{
    NSTimeInterval animationDuration = 0.30f;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    CGRect rect = CGRectMake(0.0f, 0.0f, self.view.superview.frame.size.width, self.view.superview.frame.size.height);
    self.view.superview.frame = rect;
    [UIView commitAnimations];
}

//指定分配列表行的总金额
-(CGFloat)totalMoneyByAllocationList:(NSInteger)allocationRowSelXingCan
{
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    
    return [[[modelIns.arrAllocationList objectAtIndex:allocationRowSelXingCan] objectForKey:MM_ALLOCATION_REPAIR_MONEY_KEY_STR_] floatValue];
}

@end
